I've been working on a problem that involves higher order functions and callbacks. The callback function is below.
**const addTwo = num => num + 2;**
I'm trying to pass this callback into the higher order function below.
**const map = (array, callback) => {
let newArray = [];
for (let i = 0; i < array.length; i++) {
newArray.push(callback(newArray[i]))
}
return newArray;
};**
The variable "newArray," inside of the map function should be returned as an array of 3 different numbers added by two. The arguments for the map function's parameters are below within a console log.
**console.log(map([1, 2, 3], addTwo));**
The addTwo function should become the argument of the callback parameter within the map function, this should make every element in the above array be added by two. I keep seeing [NaN, NaN, NaN] in the console and I'm not quite sure why every number in the index isn't being added by two.
I would appreciate the help.
You are pushing the values from the new array, when it should be the argument instead
**const map = (array, callback) => {
let newArray = [];
for (let i = 0; i < array.length; i++) {
newArray.push(callback(array[i]))
}
return newArray;
};**